﻿#define _CRT_SECURE_NO_WARNINGS 1

/*
栈是后进先出，队列是先进先出，这一块知识体系有所欠缺，可以考虑多复习
参考博客：
https://blog.csdn.net/Extreme_wei/article/details/129768061
*/


//每日一题_03
//232. 用栈实现队列
//https://leetcode.cn/problems/implement-queue-using-stacks/description/


class MyQueue {
public:
    // 构造函数
    MyQueue() : Stack1(), Stack2() {}

    // 直接插入数据到栈1
    void push(int x) { Stack1.push(x); }

    // 删除队列实际是删除栈2元素
    int pop() {
        // 如果栈2是空的才插入
        if (Stack2.empty()) {
            // 栈1不为空插入到栈2
            while (!Stack1.empty()) {

                Stack2.push(Stack1.top());
                Stack1.pop();
            }
        }
        int tmp = Stack2.top();
        Stack2.pop();

        return tmp;
    }

    // 返回队列开头的元素就是返回栈2的元素
    int peek() {
        // 如果栈2是空的才插入
        if (Stack2.empty()) {
            // 栈1不为空插入到栈2
            while (!Stack1.empty()) {

                Stack2.push(Stack1.top());
                Stack1.pop();
            }
        }
        return Stack2.top();
    }

    // 两个栈都为空则空
    bool empty() { return Stack1.empty() && Stack2.empty(); }

private:
    stack<int> Stack1;
    stack<int> Stack2;
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */